Ext.namespace('VAS');

VAS.TemplateMenuWindow = function($config) {
	
	this.confit  = $config;
	var _this = this;

	var $height;
	_this._getHeight = function(_height,_y) {
		if (typeof(_height) !== "undefined") {
			if (typeof(_height)==='string'&&_height.indexOf('%') > -1) {
				$height = document.body.clientHeight -_y-190;			
			} else if(typeof(_height)==='number'){
				$height = _height - 50;
			}
		} 
		
		return $height;
	}	
	
	
	var treePanel = new Ext.ux.tree.RemoteTreePanel({
		id            : $config.menuPanel.id,
		autoScroll    : true,
		rootVisible   : true,
		enableC2P     : $config.menuPanel.enableC2P || false,
		height        : _this._getHeight($config.height,$config.y),
		root          : {
			nodeType      : 'async',
			id            : 'root',
			text          : '模板管理',
			expanded      : true,
			uiProvider    : false
		},
		loader        : {
			url              : $config.menuPanel.url,
			preloadChildren  : true,
			baseParams       : {
				cmd              : 'getTree',
				treeTable        : 'tree',
				treeID           : 1
			}
		},
        listeners:{
        	"click" : function(node){
        		if (node.attributes.url != undefined && node.attributes.url !="null"  && node.attributes.url !="") {
	        		destroyWinArray(_this);
			        var portal = Ext.getCmp('std-panel');        
			        var postdata={panelId:node.attributes.id};
					AjaxDelegate(node.attributes.url, portal.callback, postdata,'return','');      		
        		}
        	}
        },		
		tbar:['查找:', {
			xtype                : 'trigger',
			width                : 120,
			triggerClass         : 'x-form-clear-trigger',
			onTriggerClick       : function() {
				this.setValue('');
				treePanel.filter.clear();
			},
			id                   : 'filter',
			enableKeyEvents      : true,
			listeners            : {
				keyup  : {
					buffer : 150,
					fn     : function(field, e) {
						if (Ext.EventObject.ESC == e.getKey()) {
							field.onTriggerClick();
						} else {
							var val = this.getRawValue();
							var re  = new RegExp('.*' + val + '.*', 'i');
							treePanel.filter.clear();
							treePanel.filter.filter(re, 'text');
						}
				}}
			}
		}]
	});	

	treePanel.filter = new Ext.ux.tree.TreeFilterX(treePanel);
	
	if (session) {
		session.put("remoteTree",treePanel);
	}

	var thisConfig = {
		closable      : false,
		autoScroll    : true,
		frame         : true,
		layout        : 'fit',
		items         : treePanel,
		tools         : [{
			id:'refresh',
			handler:function() {
				treePanel.actions.reloadTree.execute();
			}
		}],
		listeners     : {
				beforedestroy : function() {
					if (session) {
						var remoteTree = session.get("remoteTree");
						if (typeof remoteTree != undefined && remoteTree != null) {
							session.remove("remoteTree");
						}
					}
				}
		}		
	};
	
	var _config = $config || {};
	Ext.apply(_this, Ext.apply(thisConfig, _config));
	VAS.TemplateMenuWindow.superclass.constructor.call(_this);

};
Ext.extend(VAS.TemplateMenuWindow,VAS.BasicWindow,{
	
});


Ext.reg('templatemenu-win',VAS.TemplateMenuWindow);

